import React ,{Component} from 'react';
import {
    View,
    Text,
} from 'react-native';
//自定义组件
import Login from '../userCenter/login';
import ShoppingCart from './newShoppingCart';
import MyStroage from '../common/myStroage';
import Util from '../common/util'
export default class loginCart extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.onLoginSuccess = this.onLoginSuccess.bind(this);
        this.state = {
            datas: [],
            isLogin: false,
            loaded: true,
            titleType: 1,
        };
    }

    async componentDidMount() {
        try {
            let that = this;
            let userInfoDate = await MyStroage.getStorageAllDataForKey('userinfo')
            if (userInfoDate == null) {
                that.setState({isLogin: false, loaded: false});
                return;
            }else{
                that.setState({isLogin: true, loaded: false});
            }

        } catch (err) {
            LOG(err);
        }

    }


    //登录成功
    onLoginSuccess() {
        this.setState({
            loaded: false,
            isLogin:true
        })
    }

    render() {
        let loaded=this.state.loaded;
        let isLogin=this.state.isLogin;
        return (
            <View style={{flex:1}}>
                {loaded ?
                    <View style={{flex:1}}>
                        {Util.loading()}
                    </View>
                    :
                    <View style={{flex:1}}>
                        {isLogin ?
                            <View style={{flex:1}}>
                                <ShoppingCart navigation={this.props.navigation}/>
                            </View>
                            :
                            <Login navigation={this.props.navigation} from={1}
                                   onLoginSuccess={this.onLoginSuccess}/>
                        }
                    </View>
                }
            </View>
        )
    }
}
